"""
    squeeze 函数：从数组的形状中删除单维度条目，即把shape中为1的维度去掉
    用法：numpy.squeeze(a,axis = None)
     1）a表示输入的数组；
     2）axis用于指定需要删除的维度，但是指定的维度必须为单维度，否则将会报错；
     3）axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空，则删除所有单维度的条目；
     4）返回值：数组
     5) 不会修改原数组；
"""

import numpy as np

print("#" * 40, "原始数据", "#" * 40)
x = np.arange(10).reshape(1, 1, 10, 1)
print(x.shape)
print(x)

print("#" * 40, "去掉axis=0这个维度", "#" * 40)
x_squeeze_0 = np.squeeze(x, axis=0)
print(x_squeeze_0.shape, x_squeeze_0)

print("#" * 40, "去掉axis=3这个维度", "#" * 40)
x_squeeze_3 = np.squeeze(x, axis=3)
print(x_squeeze_3.shape, x_squeeze_3)

print("#" * 40, "去掉axis=0， axis=1这两个维度", "#" * 40)
x_squeeze_0_1 = np.squeeze(x, axis=(0, 1))
print(x_squeeze_0_1.shape, x_squeeze_0_1)

print("#" * 40, "去掉所有1维的维度", "#" * 40)
x_squeeze = np.squeeze(x)
print(x_squeeze.shape, x_squeeze)

print("#" * 40, "去掉不是1维的维度，抛异常", "#" * 40)
try:
    x_squeeze = np.squeeze(x, axis=2)
    print(x_squeeze.shape, x_squeeze)
except Exception as e:
    print(e)